#include "dialogtimer.h"
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QMessageBox>
#include <QTimerEvent>
#include <QDebug>

DialogTimer::DialogTimer():m_counter1(0), m_counter2(0)
{
    m_label1 = new QLabel("TIMER1: 0", this);
    m_label2 = new QLabel("TIMER2: 0", this);
    m_setButton = new QPushButton("SET", this);
    m_stopButton = new QPushButton("STOP", this);
    m_valueLine = new QLineEdit("300", this);

    m_comboBox = new QComboBox(this);
    m_comboBox->addItem ("TIMER1", m_idTimer1);
    m_comboBox->addItem ("TIMER2", m_idTimer2);

    setLayout(new QVBoxLayout);

    QHBoxLayout *hlayout = new QHBoxLayout();
    layout()->addWidget (m_label1);
    layout()->addWidget (m_label2);
    layout()->addWidget (m_comboBox);
    hlayout->addWidget (m_valueLine);
    hlayout->addWidget (m_setButton);
    layout()->addItem (hlayout);
    layout()->addWidget (m_stopButton);

    connect(m_setButton, SIGNAL(clicked()), this, SLOT(setTimer()));
    connect(m_stopButton, SIGNAL(clicked()), this, SLOT(stopTimer()));
}

void DialogTimer::timerEvent(QTimerEvent *event)
{
    if(event->timerId() == m_idTimer1)
    {
        ++m_counter1;
        m_label1->setText("TIMER1: " +QString::number (m_counter1));
    } else if(event->timerId() == m_idTimer2)
    {
        ++m_counter2;
        m_label2->setText("TIMER2: " +QString::number (m_counter2));

    }
}

void DialogTimer::setTimer()
{
    if (m_comboBox->currentIndex() == 0)
    {
        killTimer(m_idTimer1);
        m_idTimer1 = startTimer(m_valueLine->text().toInt());
    } else if (m_comboBox->currentIndex() == 1)
    {
        killTimer(m_idTimer2);
        m_idTimer2 = startTimer(m_valueLine->text().toInt());
    }
}

void DialogTimer::stopTimer()
{
    if(m_comboBox->currentIndex() == 0)
    {
        killTimer(m_idTimer1);
    } else if(m_comboBox->currentIndex() == 1)
    {
        killTimer(m_idTimer2);
    }
}




